Exercise 1

Add the greeting.cpp program from your Lab1 folder to the project. Your instructor will show you how to do this. Here is a copy of the source code.


// greeting.cpp

// This program prints a message to greet the user.
#include iostream  // Needed to do C++ I/O
#include string    // Needed by some compilers to use strings
using namespace std;
int main() {
    string name;  // This declares a variable to
    // hold the user's name
    // Get the user's name
    cout << "Please enter your first name: ";
    cin >> name;
    // Print the greeting
    cout << "Hello, " << name << "." << endl;
    return 0;
}

Exercise 2

Read the source code. What do you think the program will display when it is run if the user enters the name Adam on line 12?

Exercise 3

Follow the instructions your instructor gives you to compile the program. You should see a message telling you the program has compiled correctly. This message will be different for each compiler, but may look something like this:

Exercise 4

Exercise 5

Exercise 1

Exercise 2

Exercise 3

Exercise 4

Exercise 5

// average.cpp

// This program finds the average of two numbers.
// It contains two errors that must be fixed.
#include iostream
using namespace std;
int main() {
    int size = 0;  // The number of values to be averaged
    double num1, num2,
        average;  // Average of num1 and num2
    // Get the two numbers
    cout << "Enter two numbers separated by one or more spaces: ";
    cin >> num1 >> num2;
    // Calculate the average
    average = num1 + num2 / size;
    // Display the average
    cout << "The average of the two numbers is: " << average << endl;
    return 0;
}

Exercise 1

Exercise 2

Exercise 3

Exercise 4

Exercise 5

Exercise 1

Exercise 2

Exercise 3

Exercise 4

Exercise 5

Exercise 1

Exercise 2

Exercise 3

Exercise 4

Exercise 5